package com.hailiao.beidou.message.bd.task

import com.hailiao.beidou.message.bd.Beidou
import com.hailiao.beidou.message.bd.constant.ProtoVersion
import com.hailiao.beidou.standard2.CCICA_Data
import com.hailiao.beidou.standard2.sendCCICA
import com.hailiao.beidou.standard3.CCICR_Data
import com.hailiao.beidou.standard3.sendCCICR
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 检查卡号任务
 * @Author d10ng
 * @Date 2024/3/5 17:49
 */
object CheckCardTask {

    private var job: Job? = null
    private val scope = CoroutineScope(Dispatchers.IO)

    @Synchronized
    fun start(ver: ProtoVersion) {
        job?.cancel()
        job = scope.launch {
            while (true) {
                with(Beidou.hub) {
                    when (ver) {
                        ProtoVersion.BD2 ->  sendCCICA(CCICA_Data())
                        ProtoVersion.BD3 -> sendCCICR(CCICR_Data())
                    }
                }
                delay(10 * 1000)
            }
        }
    }

    @Synchronized
    fun stop() {
        job?.cancel()
    }
}